<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class clientNodePageadv
    ****
    * This classs represents an advanced page of any solution
    ****
    * @package clientNodes
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    ***/
    class clientNodeShopArticlePackage extends clientNode{

        /**
         * Process inputs
         * 
         * @return void 
         **/
        public function process(){
            $data = $this->db->getRow("SELECT * FROM node_shop_package WHERE id = ".$this->object);
            $data['title']      = $this->db->getOne("SELECT title FROM pagemap WHERE id = ".$this->reference);
            $data['image_path'] = $this->db->getOne("SELECT 'files/pages/'||pm.pagetype||'__'||pm.pageid||'/' AS image_path FROM pagemap pm WHERE id = ".$this->reference);
            
            $pType              = $this->db->getOne("SELECT id FROM datatype WHERE lower(codeset) = 'shoparticle'");
            $data['articles']   = $this->db->getAll("SELECT 
                                                            a.article_id, 
                                                            sa.*,
                                                            title,
                                                            get_path(pm.nodeid)||pm.pageurl||'.html' AS url,
                                                            'files/pages/".$pType."__'||a.article_id||'/' AS image_path
                                                        FROM node_shop_package_article a 
                                                        LEFT JOIN pagemap pm ON a.article_id = pm.pageid AND pm.pagetype = ".(int)$pType." 
                                                        LEFT JOIN node_shop_article sa ON a.article_id = sa.id
                                                        WHERE package_id = ".$this->object."
                                                        ORDER BY title");
                
            //get random image
            $t = $data['articles'];
            shuffle($t);
            $data['rnd'] = array_pop($t);
                            
            //get package pricing
            $data['total_price']   = 0;
            $data['total_price_k'] = 0;
            foreach($data['articles'] as $a){
                if($_SESSION['CART'] and !$_SESSION['CART']->items[$a['article_id']]){
                    $data['total_price']   = $a['price'] + $data['total_price'];
                    $data['total_price_k'] = $a['price_kids'] + $data['total_price_k'];
                }
            }
            
            if(count($data['articles']) > 3){
                $data['discount_price'] = $data['total_price'] * 0.85;
                $data['discount_price_k'] = $data['total_price_k'] * 0.85;
            }elseif(count($data['articles']) > 2){
                $data['discount_price'] = $data['total_price'] * 0.90;
                $data['discount_price_k'] = $data['total_price_k'] * 0.90;
            }elseif(count($data['articles']) > 1){
                $data['discount_price'] = $data['total_price'] * 0.95;
                $data['discount_price_k'] = $data['total_price_k'] * 0.95;
            }else{
                $data['discount_price'] = $data['total_price'];
                $data['discount_price_k'] = $data['total_price_k'];
            }
            
            //nothing
            $this->smarty->assign('data',$data);
        }
        

        /**
         * set the action template to page.html
         * 
         * @return void
         **/
        public function setTemplate(){
            $this->setActionTemplate('shopArticlePackage.html');
        }
    }
?>